package cn.taoticket.service;

import cn.taoticket.common.Constants;
import cn.taoticket.common.EmptyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.taoticket.mapper.TaoticketImageMapper;
import cn.taoticket.pojo.TaoticketImage;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by taoticket
 */
@RestController
public class RestTaoticketImageService {

    @Resource
     private TaoticketImageMapper taoticketImageMapper;

     @RequestMapping(value = "/getTaoticketImageById",method = RequestMethod.POST)
     public TaoticketImage getTaoticketImageById(@RequestParam("id") Long id)throws Exception{
        return taoticketImageMapper.getTaoticketImageById(id);
     }

     @RequestMapping(value = "/getTaoticketImageListByMap",method = RequestMethod.POST)
     public List<TaoticketImage>	getTaoticketImageListByMap(@RequestParam Map<String,Object> param)throws Exception{
        return taoticketImageMapper.getTaoticketImageListByMap(param);
     }

     @RequestMapping(value = "/getTaoticketImageCountByMap",method = RequestMethod.POST)
     public Integer getTaoticketImageCountByMap(@RequestParam Map<String,Object> param)throws Exception{
        return taoticketImageMapper.getTaoticketImageCountByMap(param);
     }

     @RequestMapping(value = "/qdtxAddTaoticketImage",method = RequestMethod.POST)
     public Integer qdtxAddTaoticketImage(@RequestBody TaoticketImage taoticketImage)throws Exception{
        taoticketImage.setCreatedTime(new Date());
        return taoticketImageMapper.insertTaoticketImage(taoticketImage);
     }

     @RequestMapping(value = "/qdtxModifyTaoticketImage",method = RequestMethod.POST)
     public Integer qdtxModifyTaoticketImage(@RequestBody TaoticketImage taoticketImage)throws Exception{
        taoticketImage.setUpdatedTime(new Date());
        return taoticketImageMapper.updateTaoticketImage(taoticketImage);
     }
     @RequestMapping(value = "queryImageList",method = RequestMethod.POST)
    public List<TaoticketImage> queryImageList(@RequestParam("targetId")Long targetId,
                                               @RequestParam("type")Integer type,
                                               @RequestParam("category")Integer category) throws Exception {
         Map<String,Object> imageParam = new HashMap<>();
         imageParam.put("targetId",targetId);
         imageParam.put("type",type);
         imageParam.put("category",category);
         List<TaoticketImage> taoticketImageList = taoticketImageMapper.getTaoticketImageListByMap(imageParam);
        return setDefaultImageList(taoticketImageList,category,type);
     }

    /**
     * 当图片为空时给定一张默认显示的图片
     */
    private List<TaoticketImage> setDefaultImageList(List<TaoticketImage> taoticketImageList, Integer category, Integer type) {
        if(EmptyUtils.isEmpty(taoticketImageList)){
            taoticketImageList = new ArrayList<>();
            TaoticketImage taoticketImage = new TaoticketImage();
            taoticketImage.setType(type);
            taoticketImage.setCategory(category);
            taoticketImageList.add(taoticketImage);
        }
        for (TaoticketImage taoticketImage : taoticketImageList) {
            if (EmptyUtils.isEmpty(taoticketImage.getImgUrl())) {
                taoticketImage.setImgUrl(Constants.DEFAULT_CAROUSEL);//要根据type/category的不同而返回不同的默认图片
            }
        }
        return taoticketImageList;
    }
}
